package edu.nf.hotel.regiondao.impl;

import edu.nf.hotel.region.CityType;
import edu.nf.hotel.region.HotelInformation;
import edu.nf.hotel.regiondao.CityTypeDao;
import edu.nf.hotel.util.ConnUtils;
import org.nf.db.util.SqlExecutor;
import org.nf.db.util.result.ArrayListHandler;
import org.nf.db.util.result.BeanHandler;
import org.nf.db.util.result.BeanListHandler;
import org.nf.db.util.result.ColumnHandler;

import java.util.List;

/**
 * @Date 2023-06-19
 * @Author 带土
 */
public class CityTypeDaoImpl implements CityTypeDao {
    /**
     * 实现添加地区类型
     * @param cityType
     */
    @Override
    public void getAddCityType(CityType cityType) {
        //创建SQLExecute（sql执行器）,在构造方法需要传入Connection对象
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql ="insert into city_type(city_code, city_name) values(?,?)";
        //保存信息
        executor.executeUpdate(sql,cityType.getCityCode(),cityType.getCityName());
    }

    /**
     * 实现删除地区类型
     * @param cityId
     */
    @Override
    public void getDeleteCityType(int cityId) {
        //创建SQLExecute (sql执行器),在构造方法需要传入Connection对象
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql ="delete from city_type where city_id = ?";
        //保存信息
        executor.executeUpdate(sql,cityId);
    }

    /**
     * 实现查询全部地区类型
     * @param cityType
     */
    @Override
    public void getUpdateCityType(CityType cityType) {
        //创建SQLExecute(sql执行器),在构造方法需要传入Connection对象
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql ="update city_type set city_code = ?,city_name = ? where city_id = ?";
        //保存信息
        executor.executeUpdate(sql,cityType.getCityCode(),cityType.getCityName(),cityType.getCityId());
    }

    @Override
    public List<CityType> getAllCityType() {
        //创建SQLExecute(sql执行器),在构造方法需要传入Connection对象
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        BeanListHandler<CityType> handler = new BeanListHandler<>(CityType.class);
        String sql ="select city_id,city_code,city_name,city_scape_image from city_type";
        List<CityType> list = executor.executeQuery(sql, handler);
        list.forEach(cityType -> System.out.println(cityType.getCityName()));
        return list;
    }

    /**
     *实现根据城市名字查询全部信息
     * @param cityName
     * @return
     */
    @Override
    public CityType getIdAllCityType(String cityName) {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql = "select city_id,city_code,city_name,city_scape_image from city_type where city_name = ?";
        BeanHandler<CityType> handler = new BeanHandler<>(CityType.class);
        CityType cityType = executor.executeQuery(sql, handler,cityName);
        System.out.println(cityType.getCityId());
        System.out.println(cityType.getCityCode());
        System.out.println(cityType.getCityScapeImage());
        return cityType;
    }

    /**
     * 实现查询城市列表
     * @param numberOf
     * @param limit
     * @return
     */
    @Override
    public List<CityType> listCity(int numberOf, int limit) {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql = "select city_id,city_code,city_name,city_scape_image from city_type limit ?,?";
        BeanListHandler<CityType> handler = new BeanListHandler<>(CityType.class);
        return executor.executeQuery(sql,handler,numberOf,limit);
    }

    /**
     *实现查询总记录数，用于计算总页数
     * @return
     */
    @Override
    public Long count() {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql = "select count(*) from city_type";
        ColumnHandler<Long> handler = new ColumnHandler<>(1);
        return executor.executeQuery(sql,handler);

        /*//测试
        Long count = executor.executeQuery(sql, handler);
        System.out.println(count);
        return count;*/

    }
}
